package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.DepartmentDao;
import com.hxkj.domain.enums.DepartmentExceptionEnum;
import com.hxkj.domain.enums.DepartmentStatusEnum;
import com.hxkj.domain.po.Department;
import com.hxkj.service.DepartmentService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.to.PageTo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门service
 * <p>
 * Create by chenwei on 2018/5/22 11:02
 */
@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentDao departmentDao;
    @Override
    public void save(Department department) throws Exception {
        departmentDao.save(department);
    }

    @Override
    public void remove(Department department) throws Exception {
        departmentDao.delete(department);
    }

    @Override
    public void modify(Department department) throws Exception {
        departmentDao.update(department);
    }

    @Override
    public ResultDataStruct<Department> get(Long id) throws Exception {
        ResultDataStruct<Department> resultDataStruct  = new ResultDataStruct();
        if(id == null){
            resultDataStruct.setCode(DepartmentExceptionEnum.PARAMETER_ERROR.getCode());
            resultDataStruct.setMessage(DepartmentExceptionEnum.PARAMETER_ERROR.getMsg());
            return resultDataStruct;
        }
        Department result = departmentDao.get(id);
        resultDataStruct.setMessage(DepartmentStatusEnum.SUCCESS.getMsg());
        resultDataStruct.setCode(DepartmentStatusEnum.SUCCESS.getCode());
        resultDataStruct.setData(result);
        return resultDataStruct;
    }

    @Override
    public PageInfo<Department> getDepartmentList(Department department, PageTo pageParam) throws Exception {
        Page<Department> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getSize());
        List<Department> functionList = departmentDao.get(department);
        PageInfo<Department> pageInfo = new PageInfo<>(page);
        pageInfo.setList(functionList);
        return pageInfo;
    }
}
